import { hundreds } from "../../../static/js/loveData";
const util = require("../../../utils/util");
// 获取应用实例
const app = getApp();

const db = wx.cloud.database();
const uimg = db.collection("uimg");
const message = db.collection("message");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    hide: app.globalData.switch,
    openid: "",
    inviteCodeMask: false,
    bindingMask: false,
    inviteCode: "",
    code_input: "",
    o_openid: "",
    heartbeat_value: 0,
    heartbeat_value1: 0,

    hasOther: false,

    avatarUrl: "",
    nickname: "",
    avatarUrl1: "",
    nickname1: "",

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  getUserData(openid) {
    const that = this;
    uimg.where({ _openid: openid }).get({
      success: (res) => {
        if (res.data[0].o_openid) {
          that.setData({
            hasOther: true,
          });

          uimg.where({ _openid: res.data[0].o_openid }).get({
            success: (res) => {
              that.setData({
                avatarUrl: res.data[0].avatarUrl,
                nickname: res.data[0].nickname,
                heartbeat_value1: res.data[0].heartbeat_value
              });
            },
          });
        }
        that.setData({
          avatarUrl1: res.data[0].avatarUrl,
          nickname1: res.data[0].nickname,
          heartbeat_value: res.data[0].heartbeat_value,
        });
      },
    });
  },

  createInviteCode() {
    // let { openid, inviteCode } = this.data;
    // inviteCode = openid.slice(-7, -1);
    this.setData({
      inviteCodeMask: true,
      // inviteCode,
    });
  },

  closeInviteCode() {
    this.setData({
      inviteCodeMask: false,
    });
  },

  copyInviteCode() {
    const { openid } = this.data;
    wx.setClipboardData({
      data: openid,
      success: (res) => {
        wx.showToast({
          title: "复制成功",
          icon: "success",
        });
        this.setData({
          inviteCodeMask: false,
        });
      },
      fail: () => {
        wx.showToast({
          title: "复制失败，请重新复制",
          icon: "none",
        });
      },
    });
  },

  bindinput(e) {
    const { value } = e.detail;
    this.setData({
      code_input: value,
    });
  },

  clickPaste() {
    const that = this;
    wx.getClipboardData({
      success(res) {
        if (res.data == "") {
          wx.showToast({
            title: "剪切板无内容",
            icon: "none",
          });
        } else {
          let code_input = res.data.trim();
          that.setData({
            code_input,
          });
        }
      },
    });
  },
  bindingOther() {
    const that = this;
    const { code_input, openid } = that.data;
    if (code_input == "") {
      wx.showToast({
        title: "请输入邀请码",
        icon: "none",
      });
    } else {
      wx.showLoading({
        title: "Loading",
        mask: true,
      });
      uimg.where({ _openid: code_input }).get({
        success: (res) => {
          if (res.data.length == 0) {
            wx.showToast({
              title: "邀请码错误",
              icon: "none",
            });
          } else if (res.data[0]._openid == openid) {
            wx.showToast({
              title: "不可以绑定自己哦",
              icon: "none",
            });
          } else {
            that.setData({
              avatarUrl: res.data[0].avatarUrl,
              nickname: res.data[0].nickname,
              bindingMask: true,
              heartbeat_value1: res.data[0].heartbeat_value,
              o_openid: code_input
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: "操作失败，请重新绑定",
            icon: "none",
          });
        },
        complete: () => {
          wx.hideLoading();
        },
      });
    }
  },

  cancleBinding() {
    this.setData({
      bindingMask: false,
    });
  },

  sureBinding() {
    const that = this;
    let { openid, code_input, heartbeat_value, heartbeat_value1 } = that.data;
    wx.showLoading({
      title: "绑定中",
      mask: true,
    });
    heartbeat_value += 30;
    heartbeat_value1 += 30;
    // that.setData({
    //   heartbeat_value,
    //   heartbeat_value1
    // })

    uimg.where({ _openid: openid }).update({
      data: {
        o_openid: code_input,
        mySwiper: [],
        heartbeat_value
      },
      success: () => {
        wx.showToast({
          title: "情侣绑定成功",
          icon: "none",
        });
        that.setData({
          hasOther: true,
        });
        wx.setStorageSync("o_openid", code_input);

        uimg.where({ _openid: code_input }).update({
          data: {
            o_openid: openid,
            mySwiper: [],
            heartbeat_value: heartbeat_value1
          },
        });
      },
      fail: () => {
        wx.showToast({
          title: "绑定失败，请重新绑定",
          icon: "none",
        });
      },
      complete: () => {
        wx.hideLoading();
        that.setData({
          bindingMask: false,
        });

        that.getUserData(openid);
      },
    });

    util.subscribe(30, '“绑定情侣”所增加心动值', false);
  },

  relieveBinding() {
    const that = this;
    let { openid, o_openid, heartbeat_value, heartbeat_value1 } = that.data;
    console.log(o_openid);
    wx.showModal({
      title: "解绑提醒",
      content: "确定要解除与对方的情侣关系嘛？",
      showCancel: true,
      confirmColor: "#a5646d",
      success: (result) => {
        if (result.confirm) {
          wx.showLoading({
            title: "解绑中请稍后",
            mask: true,
          });

          heartbeat_value -= 30;
          heartbeat_value1 -= 30;
          // that.setData({
          //   heartbeat_value,
          //   heartbeat_value1,
          // })

          uimg.where({ _openid: openid }).update({
            data: {
              o_openid: "",
              love_time: "",
              mySwiper: [],
              memorial: [],
              hundred: hundreds,
              score: [],
              love_word: [],
              heartbeat_value
            },
            success: () => {
              uimg.where({ _openid: o_openid }).update({
                data: {
                  o_openid: "",
                  love_time: "",
                  mySwiper: [],
                  memorial: [],
                  hundred: hundreds,
                  score: [],
                  love_word: [],
                  heartbeat_value: heartbeat_value1
                },
                success: () => {
                  wx.navigateBack();
                  wx.showToast({
                    title: "解绑成功",
                    icon: "none",
                  });
                },
                fail: () => {
                  wx.showToast({
                    title: "解绑失败",
                    icon: "none",
                  });
                },
                complete: () => {
                  wx.hideLoading();
                },
              });
              that.getUserData(openid);
              that.delComment(openid);
              that.delComment(o_openid);

              that.setData({
                hasOther: false,
              });

              wx.removeStorageSync("o_openid");
            },
            fail: () => {
              wx.showToast({
                title: "网络开小差，请重新操作",
                icon: "none",
              });
            },
          });

          util.subscribe(30, '“解绑情侣”所扣除心动值', false);
        }
      },
      fail: () => { },
      complete: () => { },
    });
  },

  delComment(openid) {
    message.where({ _openid: openid }).get({
      success: res => {
        let myMsg = res.data[0].myMsg;
        myMsg.forEach(i => {
          i.comment = []
        })
        message.where({ _openid: openid }).update({
          data: {
            myMsg
          }
        })
      },
      complete: () => {
        wx.hideLoading();
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    const o_openid = wx.getStorageSync("o_openid") || '';
    if (!o_openid) {
      wx.getClipboardData({
        success(res) {
          wx.showModal({
            title: '粘贴提示',
            content: '识别到剪切板内容，是否直接用来绑定情侣？',
            complete: (ret) => {
              if (ret.cancel) { }
              if (ret.confirm) {
                that.setData({
                  code_input: res.data
                }, () => {
                  that.bindingOther();
                })
              }
            }
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const openid = wx.getStorageSync("openid");
    const o_openid = wx.getStorageSync("o_openid") || '';
    this.setData({
      theme: app.globalData.theme,
      openid,
      o_openid,
    });

    this.getUserData(openid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
